package com.xxgc.eow.result;


import com.fasterxml.jackson.annotation.JsonInclude;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * @program: springboot
 * @description: 统一返回格式
 * @author: Mr.abel(ShiJiaYi)
 **/
@Data
@Schema(name = "统一返回结果")
@AllArgsConstructor
@NoArgsConstructor
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Result<T> implements Serializable {

    private static final String SUCCESSMSG = "操作成功!";

    @Schema(description = "请求状态码")
    private int code;

    @Schema(description = "响应信息")
    private String msg;

    //属性为 空（""） 或者为 NULL 都不序列化
    @JsonInclude(JsonInclude.Include.NON_EMPTY)
    @Schema(description = "数据")
    private T data;

    @JsonInclude(JsonInclude.Include.NON_EMPTY)
    @Schema(description = "异常描述")
    private String exceptionDescription;

    public Result(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public Result(int code, String msg, String ed) {
        this.code = code;
        this.msg = msg;
        this.exceptionDescription = ed;
    }

    public Result(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public static <T> Result<T> ok(){
        return new Result<>(200,SUCCESSMSG,null);
    }

    public static <T> Result<T> okMsg(String  okMsg){
        return new Result<>(200,okMsg);
    }

    public static <T> Result<T> ok(T data){
        return new Result<>(200,SUCCESSMSG,data);
    }

    public static <T> Result<T> error(String error) {
        return new Result<>(500, error, null);
    }

    /**--------------当公司还有其他业务状态返回可以在以下添加---------------*/
    public static <T> Result<T> error(String error, String ed) {
        return new Result<>(500, error, ed);
    }

    public static <T> Result<T> error(int code, String error, String ed) {
        return new Result<>(code, error, ed);
    }

}